﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.PresentationFramework.ApplicationModel;
using Luna.WPF.ApplicationFramework;
using System.Windows.Controls;
using Caliburn.Core.Metadata;
using XWord.GUI.Presenters.Interface;
using System.ComponentModel;
using Microsoft.Practices.ServiceLocation;
using XWord.GUI.Presenters.Words;
using XWord.Fundamental.Domain;
using System.Collections;
using System.Windows.Data;

namespace XWord.GUI.Presenters.Fundamental
{
    [Singleton(typeof(IEtymaWordListPresenter))]
    public class EtymaWordListPresenter : DockingPresenter, IEtymaWordListPresenter
    {
        private readonly IServiceLocator serviceLocator;
        public EtymaWordListPresenter()
        {
            serviceLocator = ServiceLocator.Current;
            this.DisplayName = "词根相关单词";
        }

        protected override void OnInitialize()
        {
            
            if (!IsInitialized)
            {
                _etymaLibraryExplorerPresenter.PropertyChanged += new PropertyChangedEventHandler(EtymaLibraryExplorerPresenter_PropertyChanged);
                base.OnInitialize();
            }
        }

        void EtymaLibraryExplorerPresenter_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SelectedItem")
            {
                var presenter = serviceLocator.GetInstance<IBaseWordExplainPresenter>();
                if(FilertWord==null)
                    FilertWord = serviceLocator.GetInstance<WordSearchEngine>().FilterWord(_etymaLibraryExplorerPresenter.SelectedItem.Name);
                BaseWordExplain = presenter.GetBaseWordExplain(FilertWord.Cast<string>().OrderBy(s=>s).ToArray());
                var view=CollectionViewSource.GetDefaultView(BaseWordExplain);
                view.SortDescriptions.Clear();
                view.SortDescriptions.Add(new SortDescription("Key", ListSortDirection.Ascending));
                FilertWord = null;
                this.Show();
                serviceLocator.GetInstance<IDockSitePresenter>().Activate(this);
            }

        }

        

        public IList FilertWord { get; set; }

        public Hashtable _baseWordExplain;
        public Hashtable BaseWordExplain
        {
            get { return _baseWordExplain; }
            set
            {
                _baseWordExplain = value;
                NotifyOfPropertyChange("BaseWordExplain");
            }
        }

        public IEtymaLibraryExplorerPresenter _etymaLibraryExplorerPresenter { get; set; }
    }
}
